package com.winsdom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winsdom.result.PageResult;
import com.winsdom.result.Result;
import com.winsdom.vo.uniformCharge.*;
import com.winsdom.service.UnifiedChargingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 统一收费控制器
 */
@RestController
@RequestMapping("/charge")
@Api(tags = "统一收费管理接口")
@Slf4j
@Validated
public class UnifiedChargingController {

    @Autowired
    private UnifiedChargingService unifiedChargingService;

    /**
     * 分页查询客户待缴与预存余额金额信息
     *
     * @param categoryId 收费类别ID
     * @param pageNum    页码
     * @param pageSize   每页大小
     * @return 分页结果
     */
    @ApiOperation(value = "分页查询客户待缴与预存余额金额信息")
    @GetMapping("/list")
    public PageResult UserBalanceInformation(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页大小", defaultValue = "10") @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(value = "资源类别ID", required = true) @RequestParam int categoryId) {

        log.info("开始查询客户待缴与预存余额金额信息, categoryId: {}, pageNum: {}, pageSize: {}", categoryId, pageNum, pageSize);

        Page<ChargingInformationVO> result = unifiedChargingService.search(pageNum, pageSize, categoryId);

        return new PageResult(result.getTotal(), result.getRecords(), pageNum, pageSize);

    }

    /**
     * 分页查询缴费历史信息
     *
     * @param pageNum       页码
     * @param pageSize      每页大小
     * @param categoryId    资源类别ID
     * @param customerName  客户名称（可选）
     * @param invoiceNumber 发票号码（可选）
     * @return 分页结果
     */
    @ApiOperation(value = "缴费历史查询")
    @GetMapping("/history")
    public PageResult PaymentHistory(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页大小", defaultValue = "10") @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(value = "资源类别ID", required = true) @RequestParam int categoryId,
            @ApiParam(value = "客户名称") @RequestParam(required = false) String customerName,
            @ApiParam(value = "发票号码") @RequestParam(required = false) String invoiceNumber) {

        log.info("开始查询缴费历史, categoryId: {}, customerName: {}, invoiceNumber: {}, pageNum: {}, pageSize: {}",
                categoryId, customerName, invoiceNumber, pageNum, pageSize);

        Page<PaymentHistoryVO> result = unifiedChargingService.searchHistory(pageNum, pageSize, categoryId, customerName, invoiceNumber);

        return new PageResult(result.getTotal(), result.getRecords(), pageNum, pageSize);

    }

    /**
     * 应收详情
     * @return
     */
    @GetMapping("/receive")
    @ApiOperation("应收详情")
    public Result<List<BillingResponseVO>> receive(@RequestParam Integer customerId){
        return Result.success(unifiedChargingService.receive(customerId));
    }

    @GetMapping("/startCharge")
    @ApiOperation("开始收费")
    public Result<StartChargeVO> startCharge(@RequestParam List<Integer> detailIds,@RequestParam Integer customerId){
        return Result.success(unifiedChargingService.startCharge(detailIds,customerId));
    }

    @GetMapping("/billingDetails")
    @ApiOperation("根据明细 IDs 查询收费明细")
    public Result<List<BillingVO>> getBillingDetails(
            @ApiParam(value = "明细 IDs", required = true) @RequestParam List<Integer> detailIds) {
        return Result.success(unifiedChargingService.getBillingDetailsByIds(detailIds));
    }

    @PostMapping("/confirmPayment")
    @ApiOperation("确认收费并生成收款码")
    public Result<ChargeResponseVO> confirmPayment(@RequestBody ChargeRequestDTO chargeRequest) {
        ChargeResponseVO response = unifiedChargingService.confirmPayment(chargeRequest);
        return Result.success(response);
    }

    @GetMapping("/all-charge-items")
    @ApiOperation("获取所有收费类型及其对应的具体收费项目列表")
    public Result<AllChargeItemsResponseVO> getAllChargeItemsWithDetails() {
        AllChargeItemsResponseVO response = unifiedChargingService.getAllChargeItemsWithDetails();
        return Result.success(response);
    }

    @GetMapping("/invoice")
    @ApiOperation("根据客户ID获取开票信息")
    public Result<InvoiceInfoResponseVO> getInvoiceInfoByCustomerId(@RequestParam Long customerId) {
        InvoiceInfoResponseVO response = unifiedChargingService.getInvoiceInfoByCustomerId(customerId);
        return Result.success(response);
    }
}
